<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "member".
 *
 * @property integer $id
 * @property integer $project_id
 * @property integer $user_id
 */
class Member extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'member';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['project_id', 'user_id'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project_id' => '项目id',
            'user_id' => '用户id',
        ];
    }
    
    /**
     * 更新项目成员
     * @param $projectId 项目id
     * @param $membersId 需要更新的成员id
     */
    public static function updateMembers($projectId, $membersId){
        Member::deleteAll(['project_id'=>$projectId]);
        if($membersId != null){
            foreach($membersId as $memberId){
                $member = new Member();
                $member->project_id = $projectId;
                $member->user_id = $memberId;
                $member->save();
            }
        }
    }
}
